﻿using System.Collections.Generic;

namespace Zo.Extensions
{
    /// <summary>
    /// 字典扩展类。
    /// </summary>
    public static class DictionaryExtensions
    {
        /// <summary>
        /// 获取与指定键关联的值。
        /// </summary>
        /// <typeparam name="TKey">要获取其值的键的类型。</typeparam>
        /// <typeparam name="TValue">与该键相关联的值的类型。</typeparam>
        /// <param name="currentDictionary">字典。</param>
        /// <param name="key">要获取其值的键。</param>
        /// <returns>与该键相关联的值。</returns>
        public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary, TKey key)
        {
            if (currentDictionary == null)
            {
                return default(TValue);
            }

            if (!currentDictionary.ContainsKey(key))
            {
                return default(TValue);
            }

            TValue value;

            var status = currentDictionary.TryGetValue(key, out value);

            return status ? value : default(TValue);
        }
    }
}